
#import "PUXuanCaiDetailsCell.h"

@implementation PUXuanCaiDetailsCell

- (NSMutableArray<UIImageView *> *)imageViewArrM {
    if (_imageViewArrM == nil) {
        _imageViewArrM = [NSMutableArray array];
    }
    return _imageViewArrM;
}
-(UIView*)imagebgView{
    if (!_imagebgView) {
        _imagebgView=[[UIView alloc]init];
//        _headbgView.backgroundColor=[UIColor colorWithHex:0xFAFCFF];
    }
    return _imagebgView;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initViews];
    }
    return  self;
}
-(void)initViews{

    CGFloat width=_window_width;
//    self.bgView=[[UIView alloc]init];
//    self.bgView.frame=CGRectMake(12*KWidthScale, 6*KWidthScale, width-12*KWidthScale*2, 68*KWidthScale);
//    self.bgView.backgroundColor=[UIColor whiteColor];
//    self.bgView.layer.cornerRadius = 10;
//    self.bgView.layer.masksToBounds=YES;
//    [self.contentView addSubview:self.bgView];
    
    self.touImage=[[UIImageView alloc]init];
    self.touImage.contentMode=UIViewContentModeScaleAspectFill;
//    self.touImage.layer.borderColor = [UIColor whiteColor].CGColor;
//    self.touImage.layer.borderWidth = 1.0;
    self.touImage.layer.masksToBounds=YES;
    self.touImage.backgroundColor=[UIColor colorWithHex:0xDCDCDC];
    [self.contentView addSubview:self.touImage];
    
    self.nameLab = [[UILabel alloc] init];
    self.nameLab.font=[UIFont boldSystemFontOfSize:17];
    self.nameLab.textColor = [UIColor blackColor];
    [self.contentView addSubview:self.nameLab];
    
    self.LVImage=[[UIImageView alloc]init];
    self.LVImage.image=[UIImage imageNamed:@"huaLV1_不带称号"];
    [self.contentView addSubview:self.LVImage];
    
    self.neirongLab = [[UILabel alloc] init];
    self.neirongLab.text=@"内容";
    self.neirongLab.numberOfLines=0;
    self.neirongLab.font=[UIFont systemFontOfSize:15];
    self.neirongLab.textColor = [UIColor blackColor];
    [self.contentView addSubview:self.neirongLab];
    
    self.timeLab = [[UILabel alloc] init];
    self.timeLab.text=@"时间";
    self.timeLab.font=[UIFont systemFontOfSize:12];
    self.timeLab.textColor =  [UIColor colorWithHex:0x888888];
    self.timeLab.textAlignment=NSTextAlignmentLeft;
    [self.contentView addSubview:self.timeLab];
    
    
    self.zanBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [self.zanBtn setImage:[UIImage imageNamed:@"XL_ZAN"] forState:UIControlStateNormal];//XL_ZAN
    [self.zanBtn setTitleColor:[UIColor colorWithHex:0xFF729A] forState:UIControlStateNormal];
    [self.zanBtn addTarget:self action:@selector(zanAction) forControlEvents:UIControlEventTouchUpInside];
    self.zanBtn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
    self.zanBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.zanBtn setTitle:@" 喜欢" forState:UIControlStateNormal];
    [self.contentView addSubview:self.zanBtn];
    
    
//    self.qgBtn =[UIButton buttonWithType:UIButtonTypeCustom];
//    [self.qgBtn setImage:[UIImage imageNamed:@"XL_QG"] forState:UIControlStateNormal];
//    [self.qgBtn setTitleColor:[UIColor colorWithHex:0xFF729A] forState:UIControlStateNormal];
////    [self.qgBtn addTarget:self action:@selector(zanAction) forControlEvents:UIControlEventTouchUpInside];
//    self.qgBtn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
//    self.qgBtn.titleLabel.font = [UIFont systemFontOfSize:13];
//    [self.contentView addSubview:self.qgBtn];
//    [self.qgBtn setTitle:@" 求购" forState:UIControlStateNormal];

}

//计算有行距的文本的文本高度
-(CGFloat)getSpaceLabelHeight:(NSString *)str withAttrDict:(NSMutableDictionary *)dict withWidth:(CGFloat)width {
    CGSize size = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
    return size.height;
}
-(void)setmodel:(PUModel *)model isSelf:(int)isSelf{
//-(void)setModel:(XLModel *)model{
    _model=model;
    self.nameLab.text=model.username;
//    self.touImage.image=[UIImage imageNamed:model.flowers_image[0]];
    NSString *avatar=model.userimage;
    NSDictionary *saveOpen_app_config = [Config getOpenAppConfig];
    if (saveOpen_app_config!=nil) {
        NSString *cos_url=saveOpen_app_config[@"cos_url"];
        NSString *cos_auth=saveOpen_app_config[@"cos_auth"];
        if (avatar.length>cos_url.length) {
            NSString *urlSuffix = [avatar substringFromIndex:cos_url.length];
            
            NSString *timeStamp=[AES128 getNowTimeStamp];
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            NSString *str=[NSString stringWithFormat:@"hometouImage%@",model.ID];
            NSString *t=[userDefaults objectForKey:str];
            if (t!=nil&&![t isEqualToString:@""]) {
                timeStamp=t;
            }else{
                [userDefaults setObject:timeStamp forKey:str];
                [userDefaults synchronize];
            }
            NSString *urlStr=[NSString stringWithFormat:@"%@%@%@",cos_auth,urlSuffix,timeStamp];
            NSData *data = [urlStr dataUsingEncoding:NSUTF8StringEncoding];
            
            if (data) {
                NSData *md5Result = [AES128 dataWithMD5Hash:data];
                NSString *hexString = [AES128 hexStringFromData:md5Result];
                
                avatar=[NSString stringWithFormat:@"%@?sign=%@&t=%@",avatar,hexString,timeStamp];
                [self.touImage sd_setImageWithURL:[NSURL URLWithString:avatar] placeholderImage:[UIImage imageNamed:@""]];
                
            } else {
                // 转换失败
                NSLog(@"Failed to convert string to NSData.");
            }
        }
    }
    
    //设置行距，并根据文本计算高度
    ////// ////// ////// ////// ////// //////
    //富文本属性
    NSMutableDictionary *textDict = [NSMutableDictionary dictionary];
    //基本属性设置
    //字体颜色
    textDict[NSForegroundColorAttributeName] = [UIColor blackColor];
    //字号大小
    textDict[NSFontAttributeName] = [UIFont systemFontOfSize:15.0];
    //段落样式
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    //行间距
    paraStyle.lineSpacing = 3.0;
    //首行文本缩进
//  paraStyle.firstLineHeadIndent = 20.0;
    //使用
    //文本段落样式
    textDict[NSParagraphStyleAttributeName] = paraStyle;
    //赋值
    self.neirongLab.attributedText = [[NSAttributedString alloc] initWithString:model.flowers_describe attributes:textDict];
    
    self.timeLab.text=model.flowers_buyTime;
    
//    if (model.is_praise == 0) {
//        [self.zanBtn setImage:[UIImage imageNamed:@"DTDZ_N"] forState:UIControlStateNormal];
//        [self.zanBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//    }else{
//        [self.zanBtn setImage:[UIImage imageNamed:@"DTDZ_S"] forState:UIControlStateNormal];
//        [self.zanBtn setTitleColor:[UIColor colorWithHex:0xFF729A] forState:UIControlStateNormal];
//    }
//    [self.zanBtn setTitle:[NSString stringWithFormat:@" %ld", (long)model.praise_num] forState:UIControlStateNormal];
    
    self.touImage.frame=CGRectMake(12*KWidthScale, 12*KWidthScale, 48*KWidthScale, 48*KWidthScale);
    self.touImage.layer.cornerRadius = self.touImage.frame.size.height/2;
    CGSize size1 = [model.username sizeWithAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:17]}];
    CGFloat width1 = ceil(size1.width);
    self.nameLab.frame=CGRectMake(self.touImage.frame.origin.x+self.touImage.frame.size.width+12*KWidthScale, 14*KWidthScale, width1, 22*KWidthScale);
    self.LVImage.frame=CGRectMake(self.nameLab.frame.origin.x+self.nameLab.frame.size.width+4*KWidthScale, 16*KWidthScale, 36*KWidthScale, 18*KWidthScale);
    
    CGFloat size2 = [self getSpaceLabelHeight:model.flowers_describe withAttrDict:textDict withWidth: _window_width-(self.touImage.frame.origin.x+self.touImage.frame.size.width+12*KWidthScale)-12*KWidthScale];
    self.neirongLab.frame=CGRectMake(self.touImage.frame.origin.x+self.touImage.frame.size.width+12*KWidthScale,self.nameLab.frame.origin.y+self.nameLab.frame.size.height+8*KWidthScale, _window_width-(self.touImage.frame.origin.x+self.touImage.frame.size.width+12*KWidthScale)-12*KWidthScale, size2);
    
    
//    NSMutableArray *resource=[NSMutableArray arrayWithArray:model.flowers_image];
//
//    [self addSubview:self.imagebgView];
//    [self.imagebgView removeAllSubviews];
//
//    for (UIImageView *image in self.imageViewArrM) {
//        [image removeFromSuperview];
//    }
//    for (int i=0; i<resource.count; i++) {
//        UIImageView *images=[[UIImageView alloc]init];
//        images.image=[UIImage imageNamed:resource[i]];
//        images.frame=CGRectMake((92*KWidthScale+8*KWidthScale)*i, 0, 92*KWidthScale, 92*KWidthScale);
//        images.layer.masksToBounds=YES;
//        images.layer.cornerRadius = 4*KWidthScale;
//        [self.imagebgView addSubview:images];
//        [self.imageViewArrM addObject:images];
//    }
////    self.imagebgView.backgroundColor=[UIColor whiteColor];
//    self.imagebgView.frame=CGRectMake(self.touImage.frame.origin.x+self.touImage.frame.size.width+12*KWidthScale, self.neirongLab.frame.origin.y+self.neirongLab.frame.size.height+8*KWidthScale, 100*KWidthScale*3, 92*KWidthScale);
    if (model.is_praise == 0) {
        [self.zanBtn setImage:[UIImage imageNamed:@"XL_NZAN"] forState:UIControlStateNormal];
        [self.zanBtn setTitleColor:[UIColor colorWithHex:0x888888] forState:UIControlStateNormal];
    }else{
        [self.zanBtn setImage:[UIImage imageNamed:@"XL_ZAN"] forState:UIControlStateNormal];
        [self.zanBtn setTitleColor:[UIColor colorWithHex:0xFF729A] forState:UIControlStateNormal];
    }
    
    
//    self.timeLab.frame=CGRectMake(0,self.neirongLab.frame.origin.y+self.neirongLab.frame.size.height+8*KWidthScale, _window_width-12*KWidthScale, 21*KWidthScale);
//    
//    self.zanBtn.frame=CGRectMake(self.touImage.frame.origin.x+self.touImage.frame.size.width+12*KWidthScale,self.timeLab.frame.origin.y+self.timeLab.frame.size.height+8*KWidthScale, 100, 18*KWidthScale);
    
    self.timeLab.frame=CGRectMake(self.touImage.frame.origin.x+self.touImage.frame.size.width+12*KWidthScale,self.neirongLab.frame.origin.y+self.neirongLab.frame.size.height+8*KWidthScale, _window_width-12*KWidthScale, 21*KWidthScale);
    
    self.zanBtn.frame=CGRectMake(_window_width-100-12*KWidthScale,self.neirongLab.frame.origin.y+self.neirongLab.frame.size.height+8*KWidthScale, 100, 18*KWidthScale);
    
    self.qgBtn.frame=CGRectMake(_window_width-100-12*KWidthScale,self.timeLab.frame.origin.y+self.timeLab.frame.size.height+8*KWidthScale, 100, 18*KWidthScale);
    
    self.rowHeight=self.zanBtn.frame.origin.y+self.zanBtn.frame.size.height+8*KWidthScale;
    
    self.LVImage.image=[UIImage imageNamed:[NSString stringWithFormat:@"huaLV%d_不带称号",model.random]];
    
    if (isSelf==1) {
        self.zanBtn.hidden=YES;
        self.qgBtn.hidden=YES;
    }else{
        self.zanBtn.hidden=NO;
        self.qgBtn.hidden=NO;
    }
}
-(void)zanAction{
    if ([Config getToken]==nil) {
        if ([self.delegate respondsToSelector:@selector(noLogin)]) {
            [self.delegate noLogin];
        }
        return;
    }
    
    NSDictionary *param =@{
                            @"id":_model.ID,
                           };
    NSString *path=@"/dynamic/praise";
    NSString *url1 = [NSString stringWithFormat:@"%@%@",baseUrl,path];
    [[AFNetManager shareManager] postDataWithURLString:url1 path:path parameters:param success:^(NSURLResponse *operation, id successObject) {
        
        NSString *result=successObject[@"result"];
        NSDictionary *dic=@{};
        if ([successObject[@"result"] isKindOfClass:[NSString class]]) {
            //解密Base64编码的字符串
            NSString *decryptedString = [AES128 decryptString:result key:dataKey iv:dataIv];
            dic=[AES128 dictionaryWithJsonString:decryptedString];
            NSLog(@"json解析===%@",dic);
        }
        
        if ([successObject[@"code"] integerValue] == 200) {
            
            int is_praise=[dic[@"status"] intValue];
            if (is_praise==1) {//点赞成功
                self.model.is_praise=1;
                [self.zanBtn setImage:[UIImage imageNamed:@"XL_ZAN"] forState:UIControlStateNormal];
                [self.zanBtn setTitleColor:[UIColor colorWithHex:0xFF729A] forState:UIControlStateNormal];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"is_praiseNotification" object:self userInfo:@{@"is_praise":@"1",@"ID":self.model.ID}];
            }else{//取消点赞成功
                self.model.is_praise=0;
                [self.zanBtn setImage:[UIImage imageNamed:@"XL_NZAN"] forState:UIControlStateNormal];
                [self.zanBtn setTitleColor:[UIColor colorWithHex:0x888888] forState:UIControlStateNormal];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"is_praiseNotification" object:self userInfo:@{@"is_praise":@"0",@"ID":self.model.ID}];
            }
            
        } else {
            [self showSVProgressHUDStr:successObject[@"message"]];
        }
      
    } failure:^(NSError *error) {
        NSLog(@"错误：%@",error);
        
    }];
}
-(void)showSVProgressHUDStr:(NSString *)str{
    if ([str isKindOfClass:[NSNull class]]||str==nil||[str isEqualToString:@""]) {
        return;
    }
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD showImage:[UIImage imageNamed:@""] status:str];
}
@end
